package com.dongnao.douyin.camera.filter;

import android.content.Context;
import android.opengl.GLES20;
import android.util.Log;

import com.dongnao.douyin.R;

public class BeautyFilter extends AbstractFrameFilter {

    private int width;
    private int height;

    public BeautyFilter(Context context) {
        super(context, R.raw.base_vertex, R.raw.beauty_frag);

        //获取索引值
        width = GLES20.glGetUniformLocation(mGLProgramId, "width");
        height = GLES20.glGetUniformLocation(mGLProgramId, "height");
    }


    @Override
    public int onDrawFrame(int textureId) {
        //设置显示窗口 调用onReady 复制 mOutputWidth, mOutputHeight 即画面的大小
        GLES20.glViewport(0, 0, mOutputWidth, mOutputHeight);

        //不调用的话就是默认的操作glsurfaceview中的纹理了。显示到屏幕上了
        //这里我们还只是把它画到fbo中(缓存)
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffers[0]);

        //使用着色器
        GLES20.glUseProgram(mGLProgramId);
        GLES20.glUniform1i(width,mOutputWidth);//mOutputWidth通过width的索引值 传递给GLES
        GLES20.glUniform1i(height,mOutputHeight);
        //传递坐标
        mGLVertexBuffer.position(0);
        GLES20.glVertexAttribPointer(vPosition, 2, GLES20.GL_FLOAT, false, 0, mGLVertexBuffer);
        GLES20.glEnableVertexAttribArray(vPosition);

        mGLTextureBuffer.position(0);
        //获取mGLTextureBuffer 坐标 光栅化 得到很多个点 进行片元着色器运算 即多次调用 beauty_frag.frag 每次传递新的vCoord进去?
        GLES20.glVertexAttribPointer(vCoord, 2, GLES20.GL_FLOAT, false, 0, mGLTextureBuffer);
        GLES20.glEnableVertexAttribArray(vCoord);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);



        GLES20.glUniform1i(vTexture, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

        //解绑
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        return mFrameBufferTextures[0];
    }
}
